package com.mls.guardian.executor.slave;

import com.mls.guardian.common.RemoteUtil;
import com.mls.guardian.container.SlaveContainer;
import com.mls.guardian.dataobj.ServerUpgradeInfo;
import com.mls.guardian.em.ContainerRole;
import com.mls.guardian.protocol.RemoteBody;
import com.mls.guardian.protocol.RemoteCommand;
import com.mls.guardian.protocol.body.ServerUpgradeInfoBody;
import com.mls.guardian.remoting.executor.RemoteExecutor;
import io.netty.channel.ChannelHandlerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * Created by root on 15-12-11.
 */
public class SlaveGetUpgradeVersionExcutor implements RemoteExecutor {
    private static final Logger log = LoggerFactory.getLogger(SlaveGetUpgradeVersionExcutor.class);
    private SlaveContainer container;

    public SlaveGetUpgradeVersionExcutor(SlaveContainer container) {
        this.container = container;
    }


    @Override
    public RemoteBody execute(ChannelHandlerContext ctx, RemoteCommand request) throws Exception {
        ServerUpgradeInfoBody serverUpgradeInfoBody = (ServerUpgradeInfoBody) request.getRemoteBody();
        log.info("id[{}] code[{}] exec SlaveGetUpgradeVersionExcutor  start", request.getRequestId(), request.getCode());
        serverUpgradeInfoBody.getUpgradeInfoList().clear();

        String version = container.getAppController().getUpgradeVersion();
        ServerUpgradeInfo serverUpgradeInfo = new ServerUpgradeInfo();
        serverUpgradeInfo.setIp(RemoteUtil.getLocalHostIP());
        serverUpgradeInfo.setVersion(version);
        serverUpgradeInfo.setRole(ContainerRole.SLAVE);

        serverUpgradeInfoBody.getUpgradeInfoList().add(serverUpgradeInfo);

        log.info("id[{}] code[{}] in SlaveGetUpgradeVersionExcutor slave finish", request.getRequestId(), request.getCode());

        serverUpgradeInfoBody.setSuc(true);
        return serverUpgradeInfoBody;
    }
}